index.js ➔ ... ➔ mkdirP   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
var path = require('path');
2
var fs = require('fs');
3
var _0777 = parseInt('0777', 8);
4
5
module.exports = mkdirP.mkdirp = mkdirP.mkdirP = mkdirP;
6
7 View Code Duplication
function mkdirP (p, opts, f, made) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
8
    if (typeof opts === 'function') {
9
        f = opts;
10
        opts = {};
11
    }
12
    else if (!opts || typeof opts !== 'object') {
13
        opts = { mode: opts };
14
    }
15
    
16
    var mode = opts.mode;
17
    var xfs = opts.fs || fs;
18
    
19
    if (mode === undefined) {
20
        mode = _0777 & (~process.umask());
21
    }
22
    if (!made) made = null;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
23
    
24
    var cb = f || function () {};
25
    p = path.resolve(p);
26
    
27
    xfs.mkdir(p, mode, function (er) {
28
        if (!er) {
29
            made = made || p;
30
            return cb(null, made);
31
        }
32
        switch (er.code) {
33
            case 'ENOENT':
34
                mkdirP(path.dirname(p), opts, function (er, made) {
35
                    if (er) cb(er, made);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
36
                    else mkdirP(p, opts, cb, made);
37
                });
38
                break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
39
40
            // In the case of any other error, just see if there's a dir
41
            // there already.  If so, then hooray!  If not, then something
42
            // is borked.
43
            default:
44
                xfs.stat(p, function (er2, stat) {
45
                    // if the stat fails, then that's super weird.
46
                    // let the original error be the failure reason.
47
                    if (er2 || !stat.isDirectory()) cb(er, made)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
48
                    else cb(null, made);
49
                });
50
                break;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
51
        }
52
    });
53
}
54
55 View Code Duplication
mkdirP.sync = function sync (p, opts, made) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
56
    if (!opts || typeof opts !== 'object') {
57
        opts = { mode: opts };
58
    }
59
    
60
    var mode = opts.mode;
61
    var xfs = opts.fs || fs;
62
    
63
    if (mode === undefined) {
64
        mode = _0777 & (~process.umask());
65
    }
66
    if (!made) made = null;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
67
68
    p = path.resolve(p);
69
70
    try {
71
        xfs.mkdirSync(p, mode);
72
        made = made || p;
73
    }
74
    catch (err0) {
75
        switch (err0.code) {
76
            case 'ENOENT' :
77
                made = sync(path.dirname(p), opts, made);
78
                sync(p, opts, made);
79
                break;
80
81
            // In the case of any other error, just see if there's a dir
82
            // there already.  If so, then hooray!  If not, then something
83
            // is borked.
84
            default:
85
                var stat;
86
                try {
87
                    stat = xfs.statSync(p);
88
                }
89
                catch (err1) {
90
                    throw err0;
91
                }
92
                if (!stat.isDirectory()) throw err0;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
93
                break;
94
        }
95
    }
96
97
    return made;
98
};
99